﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using GheSofa.Models;

namespace GheSofa.Controllers
{
    public class CartController : Controller
    {
        private GheSofaEntities db = new GheSofaEntities();

        public RedirectToRouteResult giohangthem(string masp, int soluong = 1)
        {
            List<SanPham> listsp = new List<SanPham>();
            listsp = (from sp in db.SanPhams select sp).ToList();
            foreach (SanPham item in listsp)
            {
                if (item.MaSP.ToString().Trim().Equals(masp.Trim()))
                    GetCart().AddItem(item.MaSP.ToString(), item.TenSP, (int)item.Gia, soluong);
            }
            return RedirectToAction("Index");
        }

        public RedirectToRouteResult xoasanpham(string masanpham)
        {
            GetCart().RemoveLine(masanpham);
            return RedirectToAction("Index");
        }

        public bool IsNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }

        public RedirectToRouteResult capnhatsoluong(string masanpham, string soluong)
        {
            //int soluongton = 0;
            //DateTime _ngay = DateTime.Now;
            //int _masach = int.Parse(masanpham.ToString());
            //BIZ_tonkho tk = new BIZ_tonkho();
            //tk.ngay = DateTime.Now.Date;
            //tk.sach = new BIZ_sach();
            //tk.sach.masach = int.Parse(masanpham.ToString());
            //soluongton = tk.soluongtonkho(tk);
            //if (IsNumber(soluong))
            //{
            //    if (int.Parse(soluong) <= soluongton)
            //    {
            //        if (int.Parse(soluong) < 1)
            //            GetCart().capnhatsoluong(masanpham, 1);
            //        else
            //            GetCart().capnhatsoluong(masanpham, int.Parse(soluong));
            //    }
            //    else { GetCart().capnhatsoluong(masanpham, soluongton); }
            //}
            //else
            //{
            //    GetCart().capnhatsoluong(masanpham, 1);
            //}
            return RedirectToAction("Index");
        }

        public RedirectToRouteResult xoahet()
        {
            GetCart().Clear();
            return RedirectToAction("Index");
        }

        [ChildActionOnly]
        public ActionResult menugiohang()
        {
            return PartialView(new CartIndexViewModel { Cart = GetCart() });
        }

        private Cart GetCart()
        {
            Cart cart = (Cart)Session["Cart"];
            if (cart == null)
            {
                cart = new Cart();
                Session["Cart"] = cart;
            }
            return cart;
        }

        [ChildActionOnly]
        public ActionResult footergiohang()
        {
            return PartialView();
        }
        public ActionResult thanhtoangiohang()
        {
            if (GetCart().Lines.Count() > 0)
            {
                return View(new CartIndexViewModel
                {
                    Cart = GetCart()
                });
            }
            return RedirectToAction("index");
        }

        Cart cart = new Cart();

        public RedirectToRouteResult save(FormCollection f, string tongtien)
        {
            HoaDon hd = new HoaDon();
            string user = (string)Session["User"];
            KhachHang kh = db.KhachHangs.Where(k => k.TenTK.Trim() == user.Trim()).FirstOrDefault();
            hd.MaHD = SinhMa(); //Sinh ma
            hd.MaKH = kh.MaKH;
            hd.MaNV = "NV01";
            hd.TongTien = int.Parse(tongtien);
            hd.NgayLap = DateTime.Now.Date;
            hd.NgayGiao = DateTime.Now.Date;
            hd.TinhTrang = "chưa liên lạc";
            GetCart().save(hd);
            GetCart().Clear();
            return RedirectToAction("trangchu", "home");
        }

        private string SinhMa()
        {
            string ma = "";
            List<HoaDon> list = db.HoaDons.ToList();
            HoaDon end = new HoaDon();
            if(list.Count !=0)
            {
                foreach(HoaDon hd in list)
                {
                    end = hd;
                }
                int so = int.Parse(end.MaHD.Substring(2, 2)) + 1;
                if (so < 10)
                {
                    ma = "HD0" + so.ToString();
                }
                else
                {
                    ma = "HD" + so;
                }
                return ma;
            }
            return "HD01";
        }

        public ViewResult Index()
        {
            return View(new CartIndexViewModel
            {
                Cart = GetCart()
            });
        }
    }
}
